﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AlexPilotti.L2SQLDataModel;
using AlexPilotti.L2SQLDataModel.SchemaInfo;
using System.IO;
using System.Reflection;

namespace AlexPilotti.L2SDataModelGenerator.Test
{    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {                       
            string connStr = @"Data Source=.\MSSQL08;Integrated Security=SSPI;Initial Catalog=Northwind";
            //string connStr = @"Data Source=.\MSSQL08;Integrated Security=SSPI;Initial Catalog=AdventureWorks2008";
            
            Worker w = new Worker();
            w.HandleError += new DBSchemaInfoErrorEventHandler(remoteWorker_HandleError);

            //w.GenerateAssembly(connStr, "AdventureWorks2008", Path.GetTempPath(), "AdventureWorks2008");
            w.GenerateAssembly(connStr, "Northwind", Path.GetTempPath(), "Northwind");
            
            /*
            AppDomain ad = AppDomain.CreateDomain("DataModelGeneratorTempAppDomain", AppDomain.CurrentDomain.Evidence);
            try
            {
                Worker remoteWorker = (Worker)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "AlexPilotti.L2SDataModelGenerator.Test.Worker");
                remoteWorker.HandleError += new DBSchemaInfoErrorEventHandler(remoteWorker_HandleError);
                remoteWorker.GenerateAssembly(connStr, "Northwind", Path.GetTempPath(), "TestNS");
            }
            finally
            {
                AppDomain.Unload(ad);
            }
             */ 
            
/*
            using (MSSQLDBSchemaInfo si = new MSSQLDBSchemaInfo(connStr))
            {
                IList<TableInfo> ti = si.GetTablesInfo(null);
               
                L2SQLDataModelGenerator dcg = new L2SQLDataModelGenerator();
                dcg.GenerateAssembly("northwind", Path.GetTempPath(), si, "TestNS");

                Assembly asm = Assembly.LoadFrom(Path.Combine(Path.GetTempPath(), "northwind.dll"));

                Type t = asm.GetType("TestNS.MainDataContext");
                object o = t.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { connStr });

            }            
 */ 
        }

        void remoteWorker_HandleError(object sender, DBSchemaInfoErrorEventArgs e)
        {
            Console.WriteLine(e.Exception.ToString());
        }
    }

    class Worker : MarshalByRefObject
    {
        public event DBSchemaInfoErrorEventHandler HandleError;

        public void GenerateAssembly(string connectionString, string assemblyName, string assemblyPath, string ns)
        {
            IDBSchemaInfo si = MSSQLDBSchemaInfoFactory.CreateSchemaInfo(connectionString);

            try
            {
                L2SQLDataModelGenerator dcg = new L2SQLDataModelGenerator();

                si.HandleError += new DBSchemaInfoErrorEventHandler(si_HandleError);
                dcg.HandleError += new DBSchemaInfoErrorEventHandler(si_HandleError);

                dcg.GenerateAssembly(assemblyName, assemblyPath, si, ns, true, true, true, null);
            }
            finally
            {
                if (si is IDisposable)
                    (si as IDisposable).Dispose();
            }
        }

        void si_HandleError(object sender, DBSchemaInfoErrorEventArgs e)
        {
            if (HandleError != null)
                HandleError(this, e);
        }
    }
}
